<template>
	<view class="inner-app-info bg-f1">
		<view class="top-block"></view>
		<view class="section">
			<view class="line">
				<text class="lbl">旧密码</text>
				<view class="content content-text">
					<input class="banner-input" type="password" v-model="oldPwd" placeholder="请输入旧密码" />
				</view>
			</view>
			<view class="line">
				<text class="lbl">新密码</text>
				<view class="content content-text">
					<input class="banner-input" type="password" v-model="Pwd" placeholder="请输入新密码" />
				</view>
			</view>
			<view class="line">
				<text class="lbl f2t4">确认密码</text>
				<view class="content content-text">
					<input class="banner-input" type="password" v-model="Pwd1" placeholder="请再次输入新密码" />
				</view>
			</view>
		</view>
		<u-button class="bottom" type="primary" @click="submit" :disabled="disabled">
			确定保存
		</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				disabled: false,
				oldPwd: '',
				Pwd: '',
				Pwd1: '',
			}
		},
		onLoad(options) {
			
		},
		methods: {
			// 校验表单
			validateForm() {
				if (!this.oldPwd) {
					vk.toast('请输入旧密码')
					return false;
				}
				// if (this.oldPwd != vk.getVuex('$user.userInfo.password')) {
				// 	vk.toast('旧密码错误，请重新输入')
				// 	return false;
				// }
				if (!this.Pwd) {
					vk.toast('请输入新密码')
					return false;
				}
				if (!this.Pwd1) {
					vk.toast('请输入确认密码')
					return false;
				}
				if (this.Pwd != this.Pwd1) {
					vk.toast('新密码和确认密码不一致，请重新输入')
					return false;
				}
				if (this.Pwd == this.oldPwd) {
					vk.toast('新密码和旧密码相同，请重新输入新密码')
					return false;
				}
				return true;
			},
			submit() {
				this.disabled = true;
				if (this.validateForm()) {
					let data = {
						remark: this.oldPwd,
						password: this.Pwd,
					};
					
					this.$util.request(this.$api.UpdatePwd, data, 'PUT')
						.then(res => {
							console.log('修改密码res', res)
							if (res.code == 200) {
								vk.toast("修改密码成功", "success", 1000, true, () => {
									const eventChannel = this.getOpenerEventChannel();
									if (eventChannel.emit) eventChannel.emit('refresh');
									vk.navigateBack();
								});
							} else {
								this.disabled = false;
								console.log('修改密码失败', res)
								vk.alert(res.msg, '修改密码失败')
							}
						})
						.catch(err => this.disabled = false)
				}else this.disabled = false;
			},
		}
	}
</script>

<style scoped lang="scss">
	$theme-color: rgb(63, 167, 252);

	.section {
		margin: 10px auto 0;
		padding: 0 10px 10px;
		width: 90%;
		border-radius: 10px;
		background-color: #fff;
		box-shadow: 2px 2px 6px 0 #ccc;
		box-sizing: border-box;
	}

	.line {
		margin: 20rpx 0;
		border: none;
	}

	.lbl {
		color: #aaa;
		font-weight: 400;

		&:after {
			content: '：';
			display: inline;
		}
	}

	.content-text {
		color: #333;
	}

	.banner-input {
		margin: 0 10px 0 5px;
		flex: 1;
		height: 30px;
		background-color: #fff;
		font-size: 14px;
		text-indent: 1em;
		border: 1px solid #aaa;
	}
</style>
